import { checkHttpStatus, parseJSON } from '../utils';
import { LOGOUT_USER_SUCCESS, LOGOUT_USER_REQUEST } from '../contants/index';
import { browserHistory } from 'react-router'


export function logoutUserRequest() {
  return {
    type: LOGOUT_USER_REQUEST
  }
}

export function logoutUserSuccess(session) {
  localStorage.clear();
  sessionStorage.clear();
  return {
    type: LOGOUT_USER_SUCCESS,
    payload: {
      session: session
    }
  }
}

export function logoutUser() {
  return function(dispatch) {
    dispatch(logoutUserRequest());
    return fetch("http://fengke.net:8081/web/logout", {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    })
      .then(checkHttpStatus)
      .then(parseJSON)
      .then(
        response => {
          if(response.status == 1) {
            dispatch(logoutUserSuccess(response.data));
          }
        })
  }
}